home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Online / NNTPd / server / active.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-11-01  |  3.4 KB  |  174 lines

  1. #ifndef lint
  2. static char    sccsid[] = "@(#)active.c    1.12    (Berkeley) 1/11/88";
  3. #endif
  4.  
  5. #include "common.h"
  6.  
  7. /*
  8.  * Routines to deal with the active file
  9.  */
  10.  
  11. int    act_cmp();
  12.  
  13. /*
  14.  * read_groups -- read the active file into memory, sort it,
  15.  *    and return the number of newsgroups read in.
  16.  *    If FASTFORK is true, this can be called by interrupt,
  17.  *    and may have to free up the old storage.  We decide
  18.  *    this by the fact that "num_groups" will be non-zero if
  19.  *    we're here on an interrupt.
  20.  *
  21.  *    Parameters:    None.
  22.  *
  23.  *    Returns:    Number of newsgroups read into
  24.  *            memory.
  25.  *            Zero on error.
  26.  *
  27.  *    Side effects:    Reads newsgroups into "group_array"
  28.  *            and sorts them.
  29.  */
  30.  
  31. int
  32. read_groups()
  33. {
  34.     register int    i;
  35.     register int    act_fd;
  36.     register char    *cp, *end;
  37.     char        *malloc();
  38.     struct stat    statbuf;
  39.  
  40.     /*
  41.      * If we're here on an interrupt, free up all the
  42.      * previous groups.
  43.      */
  44.  
  45.     if (num_groups != 0) {
  46.         (void) free(actbuf);
  47.         (void) free(group_array);
  48.     }
  49.  
  50.     act_fd = open(activefile, O_RDONLY);
  51.     if (act_fd < 0) {
  52. #ifdef SYSLOG
  53.         syslog(LOG_ERR, "read_groups: open %s: %m", activefile);
  54. #endif
  55.         return (0);
  56.     }
  57.  
  58.     if (fstat(act_fd, &statbuf) < 0) {
  59. #ifdef SYSLOG
  60.         syslog(LOG_ERR, "read_groups: fstat: %m");
  61. #endif
  62.         (void) close(act_fd);
  63.         return (0);
  64.     }
  65.  
  66.     actbuf = malloc(statbuf.st_size);
  67.     if (actbuf == NULL) {
  68. #ifdef SYSLOG
  69.         syslog(LOG_ERR, "read_groups: malloc %d bytes: %m",
  70.             statbuf.st_size);
  71. #endif
  72.         (void) close(act_fd);
  73.         return (0);
  74.     }
  75.  
  76.     if (read(act_fd, actbuf, (iolen_t)statbuf.st_size) != statbuf.st_size) {
  77. #ifdef SYSLOG
  78.         syslog(LOG_ERR, "read_groups: read %d bytes: %m",
  79.             statbuf.st_size);
  80. #endif
  81.         (void) close(act_fd);
  82.         return (0);
  83.     }
  84.  
  85.     (void) close(act_fd);
  86.  
  87.     for (i = 0, cp = actbuf, end = actbuf + statbuf.st_size; cp < end; cp++)
  88.         if (*cp == '\n')
  89.             i++;
  90.  
  91.     group_array = (char **) malloc(i * (sizeof (char *)));
  92.     if (group_array == NULL) {
  93. #ifdef SYSLOG
  94.         syslog(LOG_ERR, "read_groups: malloc %d bytes: %m",
  95.             i * sizeof (char **));
  96. #endif
  97.         (void) close(act_fd);
  98.         return (0);
  99.     }
  100.  
  101.     cp = actbuf;
  102.     i = 0;
  103.     while (cp < end) {
  104.         group_array[i++] = cp;
  105.         cp = index(cp, '\n');
  106.         if (cp == NULL)
  107.             break;
  108.         *cp = '\0';
  109.         cp++;
  110.     }
  111.  
  112.     qsort((char *) group_array, i, sizeof (char *), act_cmp);
  113.  
  114.     return (i);
  115. }
  116.  
  117. int
  118. act_cmp(ptr1, ptr2)
  119.     char     **ptr1, **ptr2;
  120. {
  121.     return(strcmp(*ptr1, *ptr2));
  122. }
  123.  
  124.  
  125. /*
  126.  * find_group -- find a given newsgroup and return
  127.  *    the low and high message numbers in the group
  128.  *    (space provided by user).
  129.  *
  130.  *    Parameters:    "group" is the name of the group
  131.  *            we're searching for.
  132.  *            "low_msg" and "high_msg" are
  133.  *            pointers to where we're supposed
  134.  *            to put the low and high message numbers.
  135.  *
  136.  *    Returns:    the index # in the group_array[] if all goes well;
  137.  *            -1 if we can't find the group.
  138.  *
  139.  *    Side effects:    None.
  140.  */
  141.  
  142. int
  143. find_group(group, low_msg, high_msg)
  144.     char        *group;
  145.     int        *low_msg, *high_msg;
  146. {
  147.     char        kludgebuf[MAXBUFLEN];
  148.     int        cond;
  149.     register int    low, high, mid;
  150.     int        length;
  151.  
  152.     low = 0;
  153.     (void) strcpy(kludgebuf, group);
  154.     (void) strcat(kludgebuf, " ");
  155.     length = strlen(kludgebuf);
  156.  
  157.     read_again();    /* read active file if it has changed */
  158.  
  159.     high = num_groups-1;
  160.     while (low <= high) {
  161.         mid = (low + high) / 2;
  162.         if ((cond = strncmp(kludgebuf, group_array[mid], length)) < 0)
  163.             high = mid - 1;
  164.         else if (cond > 0)
  165.             low = mid + 1;
  166.         else {
  167.             (void) sscanf(group_array[mid], "%s %d %d",
  168.                 kludgebuf, high_msg, low_msg);
  169.             return(mid);
  170.         }
  171.     }
  172.     return(-1);
  173. }
  174.